package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;


@Service
public class ItemServiceImpl implements ItemService {
    //实现远程调用
    @Autowired
    private ProductFeignClient productFeignClient;
    //调用线程池
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        //声明一个map集合
        Map<String, Object> map = new HashMap<>();
        //整合allOf：等待所有任务完成  将单线程改造成多线程同时行进
        //获取skuInfo  相当于FutureA    B.a  C.a
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1、根据skuid来获取skuInfo对象 这个对象中有skuInfo + skuImage数据
            SkuInfo skuInfo = this.productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        //2、通过三级分类获取数据
        CompletableFuture<Void> categoryViewFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        }, threadPoolExecutor);

        //3、获取价格   价格无返回值用runAsync
        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        }, threadPoolExecutor);
        //4、获取海报
        CompletableFuture<Void> spuPosterListFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            List<SpuPoster> spuPosterBySpuId = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            map.put("spuPosterList", spuPosterBySpuId);
        }, threadPoolExecutor);

        //5、获取销售属性+销售属性值
        CompletableFuture<Void> spuSaleAttrList1Future = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        //6、获取到Json数据{“3719|3730”：“22”}
        CompletableFuture<Void> valuesSkuJson1Future = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            //将map转换成json
            String valuesSkuJson = JSON.toJSONString(skuValueIdsMap);
            map.put("valuesSkuJson", valuesSkuJson);

        }, threadPoolExecutor);

        //7、获取规格参数   无返回值
        CompletableFuture<Void> spuSaleAttrListFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> spuSaleAttrList = productFeignClient.getAttrList(skuId);
            List<HashMap<String, Object>> skuAttrList = spuSaleAttrList.stream().map(baseAttrInfo -> {
                //声明一个map集合
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("attrName", baseAttrInfo.getAttrName());
                hashMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return hashMap;
            }).collect(Collectors.toList());
            map.put("skuAttrList", skuAttrList);
        }, threadPoolExecutor);
        //8、更新商品incrHotScore  远程调用无返回值
        CompletableFuture<Void> incrHotScoreFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        //查询完成后 使用多任务组合allOf
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewFuture,
                priceFuture,
                spuPosterListFuture,
                spuSaleAttrList1Future,
                valuesSkuJson1Future,
                spuSaleAttrListFuture,
                incrHotScoreFuture
        ).join();
        return map;
    }
}
